﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Windows.Input;
using ViewMaker.Core.Controls;
using ViewMaker.Core.Utils;

namespace ViewMaker.Core.ViewModels
{
    [View(ViewControlType.Grid, Title = "String List Edtior")]
    [ViewProperty(GridViewControl.Properties.Margin, "10")]
#if !SILVERLIGHT
    [ViewProperty(GridViewControl.Properties.WindowResizeMode, System.Windows.ResizeMode.NoResize)]
#endif
    [ViewProperty(GridViewControl.Properties.ItemWidth, 200)]
    [ViewProperty(GridViewControl.Properties.ItemMargin, "0,0,0,5")]
    public class StringListEditViewModel : ViewModelBase, IViewLayoutGeneratorProvider
    {
        private const int ButtonWidth = 60;

        [Browsable(false)]
        public ObservableCollection<string> Data { get; private set; }

        private ObservableCollection<string> originalData;

        [Display(Name = "Item List")]
        [View(ViewControlType.ListBox)]
        [ViewProperty(ListBoxViewControl.Properties.ItemsSource, "Data")]
        [ViewProperty(ListBoxViewControl.Properties.Height, 200)]
        public string SelectedItem { get; set; }

        [Display(Name = "New Item")]
        public string NewItem { get; set; }

        [Display(Name = "Add")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand AddCommand { get { return CreateCommand(Add); } }

        [Display(Name = "Delete")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand DeleteCommand { get { return CreateCommand(Delete); } }

        [Display(Name = "Up")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand UpCommand { get { return CreateCommand(Up); } }

        [Display(Name = "Down")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand DownCommand { get { return CreateCommand(Down); } }

        [Display(Name = "Ok")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand OkCommand { get { return CreateCommand(Ok); } }

        [Display(Name = "Cancel")]
        [ViewProperty(ButtonViewControl.Properties.Width, ButtonWidth)]
        public ICommand CancelCommand { get { return CreateViewCommand(ViewCommands.Close); } }


        public StringListEditViewModel(ObservableCollection<string> list)
        {
            this.originalData = list;
            Data = new ObservableCollection<string>(list);
        }

        private void Add()
        {
            if (string.IsNullOrEmpty(NewItem)) return;
            Data.Add(NewItem);
            NewItem = "";
            OnPropertyChanged("NewItem");
        }

        private void Delete()
        {
            if (SelectedItem == null) return;
            Data.Remove(SelectedItem);
        }

        private void Up()
        {
            if (SelectedItem == null) return;
            int pos = Data.IndexOf(SelectedItem);
            if (pos == 0) return;
            Data.Move(pos, pos - 1);
        }

        private void Down()
        {
            if (SelectedItem == null) return;
            int pos = Data.IndexOf(SelectedItem);
            if (pos >= Data.Count - 1) return;
            Data.Move(pos, pos + 1);
        }

        private void Ok()
        {
            originalData.Clear();
            foreach (var item in Data)
            {
                originalData.Add(item);
            }
            ExecuteViewCommand(ViewCommands.Close);
        }


        #region IViewLayoutGeneratorProvider メンバー

        ViewLayoutItem IViewLayoutGeneratorProvider.Generate()
        {
            var generator = ServiceLocator.GetService<IViewLayoutGenerator>();
            var layout = generator.Generate(this, LayoutGenerateFlag.LayoutData | LayoutGenerateFlag.SkipLoadError);
            if (layout != null) return layout;
            layout = generator.Generate(this, LayoutGenerateFlag.TypeInfo);

            var up = layout.FindChild("UpCommand");
            var down = layout.FindChild("DownCommand");
            var itemPanel = layout.FindChild("StringListEditViewModelDataPanel");

            itemPanel.Remove();

            var subPanelControl = new StackPanelViewControl { Orientation = LayoutOrientation.Horizontal};
            var subPanel = new ViewLayoutItem(layout, "subMain", subPanelControl, null, true, null);
            layout.Children.Insert(0, subPanel);
            subPanel.AddChild(itemPanel);

            var subButtonPanelControl = new StackPanelViewControl { ItemMargin = "5" };
            var subButtonPanel = new ViewLayoutItem(subPanel, "subMainButton", subButtonPanelControl, null, true, null);
            subPanel.AddChild(subButtonPanel);
            up.Remove();
            down.Remove();
            subButtonPanel.AddChild(up);
            subButtonPanel.AddChild(down);
            return layout;
        }

        #endregion
    }
}
